package top.hedonihilist.loginexample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;

@Controller
@RequestMapping("/")
public class LoginController {

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/users")
    public String users(Model model) {
        model.addAttribute("userlist", Arrays.asList("user1", "admin"));
        return "users";
    }

    @GetMapping("/user/{id}")
    public String user_detail(@PathVariable(name="id") String id, Model model, Principal principal) {
        model.addAttribute("username", principal.getName());    // 注入Principle以获取用户名
        if (id.equals(principal.getName())) {
            model.addAttribute("userinfo", "是本人没错了，假装这是详细信息");
        } else {
            model.addAttribute("userinfo", "当前登录用户不是"+id+", 不给你看详细信息");
        }
        return "user";
    }

    @GetMapping("/profile")
    public String profile(Principal principal) {
        return "redirect:/user/" + principal.getName();
    }

    @GetMapping("/error")
    public String fail() {
        return "error";
    }
}
